package cn.acmoj.oj.controller;

import cn.acmoj.oj.bean.User;
import cn.acmoj.oj.service.UserService;
import cn.acmoj.oj.util.Util;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.sql.Timestamp;

/**
 * cn.acmoj.oj.controller
 * Springer
 * 2017-11-09
 */
@Controller
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private Gson gson;

    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String register() {
        return "register";
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(@RequestParam(required = false, defaultValue = "") String from) {
        return "login";
    }

    @RequestMapping(value = "lostpassword", method = RequestMethod.GET)
    public String lostpassword() {
        return "lostpassword";
    }

    @RequestMapping(value = "userinfo", method = RequestMethod.GET)
    public String userInfo() {
        return "userinfo";
    }

    @RequestMapping(value = "api/login", method = RequestMethod.POST)
    public void login(@RequestParam String username,
                      @RequestParam String password,
                      PrintWriter out,
                      HttpSession session) {
        User user = userService.login(username, password);
        if (user == null) {
            out.print(false);
            return;
        }
        user.setPassword("");
        session.setAttribute("user", user);
        String privilege = userService.getPrivilege(username);
        System.out.println(privilege);
        if (privilege != null) {
            session.setAttribute("privilege", privilege);
        }
        out.print(true);
    }

    @RequestMapping(value = "api/register", method = RequestMethod.POST)
    public void reg(@RequestParam(required = false) String nick,
                    @RequestParam(required = false) String email,
                    @RequestParam String username,
                    @RequestParam String password,
                    HttpServletRequest request,
                    PrintWriter out) {
        User user = new User();
        user.setUser_id(username);
        user.setNick(nick);
        user.setEmail(email);
        user.setPassword(password);
        user.setReg_time(new Timestamp(System.currentTimeMillis()));
        user.setIp(Util.getIp(request));
        user.setAccesstime(new Timestamp(System.currentTimeMillis()));
        boolean flag = userService.register(user);
        if (flag) {
            out.print(true);
        } else {
            out.print("username");
        }
    }

    @RequestMapping(value = "api/info", method = RequestMethod.POST)
    public void info(@RequestParam String nick,
                     @RequestParam String email,
                     @RequestParam String oldPassword,
                     @RequestParam String password,
                     HttpSession session,
                     PrintWriter out) {
        User user = (User) session.getAttribute("user");
        user.setNick(nick);
        user.setEmail(email);
        if (!password.isEmpty()) {
            user.setPassword(password);
        } else {
            user.setPassword(null);
        }
        boolean flag = userService.update(user, oldPassword);
        if (flag) {
            out.print(true);
        } else {
            out.print("username");
        }
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index.html";
    }
}
